//
//  QSFLoginViewController.m
//  QSF_Dev
//
//  Created by Mr.Dynasty on 2018/6/5.
//  Copyright © 2018年 Mr.Dynasty. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"

@interface LoginViewController ()

@property (strong, nonatomic) LoginView *loginView;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)makeView {
    self.view.backgroundColor = [UIColor whiteColor];
    _loginView = [[LoginView alloc] init];
    [self.view addSubview:self.loginView];
    [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    __weak typeof(self)weakself = self;
    [_loginView loginBtnClick:^(NSString *account, NSString *password) {
        [weakself netForLoginWithAccount:account password:password];
    }];
    [_loginView forgetPasswordTap:^{
        
    }];
}

- (void)netForLoginWithAccount:(NSString *)account password:(NSString *)password {
    
    AVUser *user = [AVUser user];
    user.username = account;
    user.password = password;
    
    //注册
//    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//        if (succeeded) {
//
//        }else if(error.code == 202){
//            //注册失败的原因可能有多种，常见的是用户名已经存在。
//            NSLog(@"注册失败，用户名已经存在");
//        }else{
//            NSLog(@"注册失败：%@",error.localizedFailureReason);
//        }
//    }];
    
    [AVUser logInWithUsernameInBackground:account password:password block:^(AVUser *user, NSError *error){
        if (user) {
            UserInfoModel *model = [[UserInfoModel alloc] init];
            model.isLogin = YES;
            model.name = account;
            [[UserManager shareManager] saveUserInfoModel:model];
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [app changeRootWindow];
        } else {
            NSLog(@"登录失败：%@",error.localizedFailureReason);
        }
    }];
    
    
}

@end
